package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MyMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    /**
     * 査菜单
     *
     * @return
     */
    @RequestMapping("/getAllMenu")
    public Result getAllMenu() {
        try {
            List<Menu> list = menuService.getAllMenu();
            return new Result(true, MyMessageConstant.MENU_QUERY_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MyMessageConstant.MENU_QUERY_FAIL);
        }
    }


    //菜单分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean) throws Exception {
        //菜单分页查询的调用
        PageResult page = menuService.findPage(pageBean);
        return page;
    }


    //查询所有角色的信息
    @RequestMapping("/findAll")
    public Result findAll() {

        try {
            //调用查询方法
            List<Role> roles = menuService.findAll();
            return new Result(true, MessageConstant.GET_R_SUCCESS, roles);

        } catch (Exception e) {
            e.printStackTrace();
            //查询失败，给出提示
            return new Result(false, MessageConstant.GET_R_FAIL);
        }
    }


    //新增菜单
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu, Integer[] roleIds) {

        try {
            //新增菜单的调用
            menuService.add(menu, roleIds);

            return new Result(true, MessageConstant.ADD_M_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            // 添加失败，给出提示
            return new Result(false, MessageConstant.ADD_M_FAIL);
        }
    }


    //编辑菜单时，基本数据的回显
    @RequestMapping("/findByIdBack_J")
    public Result backBaseInfor(Integer id) {

        try {
            //调用回显基本信息的方法
            Menu baseData = menuService.backBaseInfor(id);

            //执行成功，给出提示
            return new Result(true, MessageConstant.GET_M_SUCCESS, baseData);
        } catch (Exception e) {
            e.printStackTrace();

            //执行失败，给出提示
            return new Result(false, MessageConstant.GET_M_FAIL);
        }
    }


    //删除菜单
    @RequestMapping("/deleteById")
    public Result deleteMenuById(Integer id) {

        try {

            menuService.deleteMenuById(id);

            //执行删除成功，给出提示信息
            return new Result(true, MessageConstant.DELETE_M_SUCCESS);
        } catch (Exception e) {

            e.printStackTrace();

            //执行删除失败，给出提示信息
            return new Result(false, MessageConstant.DELETE_M_PAIL);
        }
    }

    //编辑菜单时，回显角色信息
    @RequestMapping("/findRoles")
    public Result findRoles(Integer id) {

        try {

            //菜单中的角色的回显。注意用集合或数组存储
            List<Integer> list = menuService.findRoles(id);

            //执行成功，给出提示
            return new Result(true, MessageConstant.GET_R_SUCCESS, list);

        } catch (Exception e) {
            e.printStackTrace();

            //执行失败，给出提示
            return new Result(false, MessageConstant.GET_R_FAIL);
        }
    }


    //编辑保存
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu, Integer[] roleIds) {

        try {
            //调用添加检查组的方法
            menuService.edit(menu, roleIds);
            //执行成功，给出提示
            return new Result(true, MessageConstant.EDIT_M_SUCCESS);

        } catch (Exception e) {
            e.printStackTrace();
            //执行失败，给出提示
            return new Result(false, MessageConstant.EDIT_M_FAIL);
        }
    }
}




